const CONFIG = {
    BASE_URL: 'http://localhost:8080',
    API: {
        USER: {
            LOGIN: '/User/Login',
            ADD: '/User/Add',
            GET_ALL: '/User/getAll',
            DELETE: '/User/Delete',
            UPDATE: '/User/Update'
        },
        MESSAGE: {
            ADD: '/Message/Add',
            SEARCH: '/Message/Search',
            GET_DETAIL: '/Message/GetDetail',
            DELETE: '/Message/Delete',
            UPDATE: '/Message/Update',
            PHOTO_UP: '/Message/photoup',
            FILE_UP: '/Message/fileup',
            FILE_CHANGE: '/Message/filechange',
            PHOTO_CHANGE: '/Message/photochange',
            FILE_DELETE: '/Message/filedelete',
            PHOTO_DELETE: '/Message/photodelete',
            FILE_FIND: '/Message/filefind',
            PHOTO_FIND: '/Message/photofind',
        },
        COMMENT: {
            ADD: '/Comment/Add',
            FIND: '/Comment/Find',
            DELETE: '/Comment/Delete',
            UPDATE: '/Comment/Update',
            VOTE: '/Comment/GetSumVote',
        }
    }
};

const MESSAGE_TYPES = {
    ANNOUNCEMENT: 0,  // 公告
    NOTICE: 1,       // 通知
    NEWS: 2          // 新闻
};

const USER_TYPES = {
    ADMIN: 0,
    EMPLOYEE: 1,
    USER: 2
};

const MESSAGE_TYPE_PERMISSIONS = {
    [USER_TYPES.ADMIN]: [MESSAGE_TYPES.ANNOUNCEMENT, MESSAGE_TYPES.NOTICE, MESSAGE_TYPES.NEWS],
    [USER_TYPES.EMPLOYEE]: [MESSAGE_TYPES.ANNOUNCEMENT, MESSAGE_TYPES.NOTICE, MESSAGE_TYPES.NEWS],
    [USER_TYPES.USER]: [MESSAGE_TYPES.NEWS]
};

// 请求工具函数
const request = async (url, options = {}) => {
    const defaultOptions = {
        headers: {
            'Content-Type': 'application/json',
            'Accept': 'application/json'
        }
    };
    
    try {
        const response = await fetch(CONFIG.BASE_URL + url, {
            ...defaultOptions,
            ...options
        });
        
        // 检查响应类型
        const contentType = response.headers.get('content-type');
        if (contentType && contentType.includes('application/json')) {
            const data = await response.json();
            if (response.ok) {
                return data;
            }
            throw new Error(data.message || 'Request failed');
        } else {
            // 如果不是JSON响应，获取文本内容
            const text = await response.text();
            const data = {
                resp: text
            }
            if(response.ok){
                return data;
            }
            throw new Error(data.message || 'Request failed');
        }
    } catch (error) {
        console.error('Request failed:', error);
        throw error;
    }
};

// API 调用封装
const API = {
    user: {
        login: (data) => request(CONFIG.API.USER.LOGIN, {
            method: 'POST',
            body: JSON.stringify(data)
        }),
        add: (data) => request(CONFIG.API.USER.ADD, {
            method: 'POST',
            body: JSON.stringify(data)
        }),
        getAll: () => request(CONFIG.API.USER.GET_ALL, {
            method: 'GET'
        }),
        delete: (uid) => request(`${CONFIG.API.USER.DELETE}/${uid}`, {
            method: 'POST'
        }),
        update: (data) => request(CONFIG.API.USER.UPDATE, {
            method: 'POST',
            body: JSON.stringify(data)
        }),
    },
    message: {
        add: (data) => request(CONFIG.API.MESSAGE.ADD, {
            method: 'POST',
            body: JSON.stringify(data)
        }),
        search: (data) => request(CONFIG.API.MESSAGE.SEARCH, {
            method: 'POST',
            body: JSON.stringify(data)
        }),
        getDetail: (data) => request(CONFIG.API.MESSAGE.GET_DETAIL, {
            method: 'POST',
            body: JSON.stringify(data)
        }),
        delete: (mid) => request(`${CONFIG.API.MESSAGE.DELETE}/${mid}`, {
            method: 'POST'
        }),
        update: (data) => request(CONFIG.API.MESSAGE.UPDATE, {
            method: 'POST',
            body: JSON.stringify(data)
        }),
        uploadPhoto: (data) => request(CONFIG.API.MESSAGE.PHOTO_UP, {
            method: 'POST',
            body: JSON.stringify(data)
        }),
        uploadFile: (data) => request(CONFIG.API.MESSAGE.FILE_UP, {
            method: 'POST',
            body: JSON.stringify(data)
        }),
        updateFile: (data) => request(CONFIG.API.MESSAGE.FILE_CHANGE, {
            method: 'POST',
            body: JSON.stringify(data)
        }),
        updatePhoto: (data) => request(CONFIG.API.MESSAGE.PHOTO_CHANGE, {
            method: 'POST',
            body: JSON.stringify(data)
        }),
        deleteFile: (data) => request(CONFIG.API.MESSAGE.FILE_DELETE, {
            method: 'POST',
            body: JSON.stringify(data)
        }),
        deletePhoto: (data) => request(CONFIG.API.MESSAGE.PHOTO_DELETE, {
            method: 'POST',
            body: JSON.stringify(data)
        }),
        fileFind: (data) => request(CONFIG.API.MESSAGE.FILE_FIND, {
            method: 'POST',
            body: JSON.stringify(data)
        }),
        photoFind: (data) => request(CONFIG.API.MESSAGE.PHOTO_FIND, {
            method: 'POST',
            body: JSON.stringify(data)
        })
    },
    comment: {
        add: (data) => request(CONFIG.API.COMMENT.ADD, {
            method: 'POST',
            body: JSON.stringify(data)
        }),
        find: (data) => request(CONFIG.API.COMMENT.FIND, {
            method: 'POST',
            body: JSON.stringify(data)
        }),
        delete: (data) => request(CONFIG.API.COMMENT.DELETE, {
            method: 'POST',
            body: JSON.stringify(data)
        }),
        update: (data) => request(CONFIG.API.COMMENT.UPDATE, {
            method: 'POST',
            body: JSON.stringify(data)
        }),
        vote: (data) => request(CONFIG.API.COMMENT.VOTE, {
            method: 'POST',
            body: JSON.stringify(data)
        })
    }
};

// 导出配置和API
window.CONFIG = {
    BASE_URL: CONFIG.BASE_URL,
    API: CONFIG.API,
    MESSAGE_TYPES,
    USER_TYPES,
    MESSAGE_TYPE_PERMISSIONS
};
window.API = API;